package com.miyake.dsp.otdr.ui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Vector;

import javax.swing.JButton;

import com.thoughtworks.xstream.XStream;

public class FileSetupXml implements FileSetup{

	@Override
	public void save(String filename, FiberNodeUI root) {
    	XStream xstream = new XStream();
    	xstream.omitField(FiberNodeUI.class, "reference"); 
    	xstream.omitField(FiberNodeUI.class, "iconActive"); 
    	xstream.omitField(FiberNodeUI.class, "iconNoneNode");
    	xstream.omitField(FiberNodeUI.class, "iconSplitter");
    	xstream.omitField(FiberNodeUI.class, "iconOTDR");
    	xstream.omitField(FiberNodeUI.class, "iconONU");
    	xstream.omitField(FiberNodeUI.class, "iconOpen");
    	xstream.omitField(FiberNodeUI.class, "iconAPC");

    	String xml = xstream.toXML(root);
    	try {
			BufferedWriter writeFile = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(filename)));	
			writeFile.write(xml);
			writeFile.close();	
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
	    }
	}

	@Override
	public FiberNodeUI load(String filename) {
		XStream xstream = new XStream();
    	
		BufferedReader reader;
		String tmp = null;
		String xml2 = "";
		FiberNodeUI root = null;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
			
			while((tmp = reader.readLine()) != null) {
				xml2 += tmp;
			}
			root = (FiberNodeUI)xstream.fromXML(xml2);
			reader.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();		
		} catch (IOException e) {
			e.printStackTrace();
		}
		return root;
	}

}
